package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRespVO;
import com.atguigu.gulimall.product.vo.AttrRespVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Resource
    private AttrService attrService;

    /**
     * @param params:    分页请求参数
     * @param catelogId: 三级分类ID
     * @Description: 获取分类属性分组
     * @Author: LiQianJin
     * @Date: 2024/3/30 15:10
     * @return: com.atguigu.common.utils.PageUtils
     **/
    @Override
    public PageUtils queryAttrGroupDetailInfoPage(Map<String, Object> params, Long catelogId) {
        String keyword = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (0 != catelogId) {
            wrapper.eq("catelog_id", catelogId);
        }
        if (!StringUtils.isEmpty(keyword)) {
            // select * from pms_attr_group where attr_group_id = keyword or attr_group_name like %keyword%
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", keyword).or().like("attr_group_name", keyword);
            });
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params), wrapper);
        PageUtils pageUtils = new PageUtils(page);
        return pageUtils;
    }

    /***
     * @Description: 获取属性分组详情
     * @Author: LiQianJin
     * @Date: 2024/3/30 15:54
     * @param attrGroupId: 分组ID
     * @return: com.atguigu.gulimall.product.vo.AttrGroupRespVO
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AttrGroupRespVO queryAttrGroupInfo(Long attrGroupId) {
        AttrGroupRespVO respVO = new AttrGroupRespVO();
        AttrGroupEntity attrGroupEntity = this.getById(attrGroupId);
        BeanUtils.copyProperties(attrGroupEntity, respVO);
        // 获取分类的完整路径 逆序查找参数225-25-2 从一级分类开始展示 2-25-225
        List<Long> categoryPath = this.categoryService.findCategoryPath(attrGroupEntity.getCatelogId());
        respVO.setCatelogPath(new Long[categoryPath.size()]);
        return respVO;
    }

    /**
     * @param attrGroupId:分组ID
     * @Description: 获取属性分组关联的所有属性
     * @Author: LiQianJin
     * @Date: 2024/3/30 18:23
     * @return: java.util.List<com.atguigu.gulimall.product.vo.AttrRespVO>
     **/
    @Override
    public List<AttrRespVO> queryAttrGroupRelationInfo(Long attrGroupId) {
        List<AttrRespVO> attrRespVOS = Collections.emptyList();
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationList = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
        if (!CollectionUtils.isEmpty(attrAttrgroupRelationList)) {
            List<Long> attrIds = attrAttrgroupRelationList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            if (null == attrIds || attrIds.size() == 0) {
                return null;
            }
            Collection<AttrEntity> attrEntities = attrService.listByIds(attrIds);
            if (CollectionUtils.isEmpty(attrEntities)) {
                attrEntities = Collections.emptyList();
            }
            attrRespVOS = attrEntities.stream().map((attr) -> {
                AttrRespVO attrRespVO = new AttrRespVO();
                //  设置属性名 可选值
                attrRespVO.setAttrName(Optional.ofNullable(attr).map(AttrEntity::getAttrName).orElse(null));
                attrRespVO.setValueSelect(Optional.ofNullable(attr).map(AttrEntity::getValueSelect).orElse(null));
                return attrRespVO;
            }).collect(Collectors.toList());
        }
        return attrRespVOS;
    }

    /**
     * @param idList: id集合
     * @Description: 批量删除
     * @Author: LiQianJin
     * @Date: 2024/3/30 19:13
     * @return: void
     **/
    @Override
    public void batchRemove(List<Long> idList) {
        this.baseMapper.deleteBatchIds(idList);
    }

}